package com.ly.heart.util;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.springframework.stereotype.Component;

@Component
public class ChineseToPinyin {

    /**
     * 汉字转拼音
     * @param chinese
     * @return
     */
    public static String changeSpell (String chinese) {
        //将汉字参数去除空格后转化为数组
        char[] chineseArr = chinese.trim ().toCharArray ();
        //定义一个字符串
        StringBuilder spell = new StringBuilder ();
        //输出格式
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        /**
         * 输出大小写设置
         * LOWERCASE:输出小写
         * UPPERCASE:输出大写
         */
        format.setCaseType ( HanyuPinyinCaseType.LOWERCASE );
        /**
         * 输出音标设置
         * WITH_TONE_MARK:直接用音标符（必须设置WITH_U_UNICODE，否则会抛出异常）
         * WITH_TONE_NUMBER：1-4数字表示音标
         * WITHOUT_TONE：没有音标
         */
        format.setToneType ( HanyuPinyinToneType.WITHOUT_TONE );
        /**
         * 特殊音标ü设置
         * WITH_V：用v表示ü
         * WITH_U_AND_COLON：用"u:"表示ü
         * WITH_U_UNICODE：直接用ü
         */
        format.setVCharType ( HanyuPinyinVCharType.WITH_U_UNICODE );

        try {
            for ( int i = 0; i < chineseArr.length; i++ ) {
                //判断是否是汉字
                if ( Character.toString ( chineseArr[i] ).matches ( "[\\u4E00-\\u9FA5]+" ) ) {
                    //如果是多音字，返回多个拼音的数组
                    String[] pys = PinyinHelper.toHanyuPinyinStringArray ( chineseArr[i], format );
                    //只取数组中的第一个
                    spell.append ( pys[0] );
                } else {
                    //如果不是汉字直接拼接到spell中
                    spell.append ( chineseArr[i] );
                }
            }
        } catch ( BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination ) {
            badHanyuPinyinOutputFormatCombination.printStackTrace ();
        }
        return spell.toString ().toLowerCase ();
    }

    /**
     * 获取首字母
     * @param chinese
     * @return
     */
    public static String getFirstLetter(String chinese) {
        char[] chineseArr = chinese.trim().toCharArray();
        StringBuilder firstLetters = new StringBuilder();

        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);

        try {
            for (int i = 0; i < chineseArr.length; i++) {
                if (Character.toString(chineseArr[i]).matches("[\\u4E00-\\u9FA5]+")) {
                    String[] pys = PinyinHelper.toHanyuPinyinStringArray(chineseArr[i], format);
                    if (pys != null && pys.length > 0 && pys[0] != null && pys[0].length() > 0) {
                        // 取第一个拼音的首字母
                        firstLetters.append(pys[0].charAt(0));
                    }
                } else {
                    // 不是汉字直接拼接
                    firstLetters.append(chineseArr[i]);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return firstLetters.toString();
    }


    public static void main(String[] args) {
        System.out.println(getFirstLetter("研发部门"));
    }


}
